<?php
namespace Blade\Cache;

class FilesystemCache {

    public function __construct(string $directory, int $options = 0)
    {
        $this->directory = rtrim($directory, '\/').'/';
        $this->options = $options;
    }

    public function generateKey(string $name)
    {
    	return $this->directory . md5($name) . '.php';
    }

    public function read(string $name): void
    {
    	//如果缓存文件存在并且未过期
        if ( file_exists($name) && @filemtime($name) > time() ) {
            @include_once $name;
        }
    }

    //写入缓存
    public function write($name, $content)
    {
    	if( @file_put_contents($name, $content) !== false ){
    		@touch($name, time()+3600);//设置过期时间
    	}

    	return false;
    }

    public function getTimestamp(string $key): int
    {
        if (!file_exists($key)) {
            return 0;
        }

        return (int) @filemtime($key);
    }

    public function exists($name)
    {
        if ( file_exists($name) && @filemtime($name) > time() ) {
            return true;
        }
        return false;
    }
}